//
//  DCChooseBank.m
//  CustomPicker
//
//  Created by Joinwe on 16/3/24.
//  Copyright © 2016年 Joinwe. All rights reserved.
//

#import "DCChooseBank.h"

@interface DCChooseBank()<UITableViewDataSource,UITableViewDelegate>


@property (nonatomic, strong) UIView *backImageView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *chooseNum;


@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;

@end

@implementation DCChooseBank

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createControl];
    }
    return self;
}


/**添加控件*/
- (void)createControl{
    
    _titleArray = @[@"北京银行",@"工商银行",@"光大银行",@"广发银行",@"建设银行",@"交通银行"];
    _imageArray = @[@"beijing",@"gs",@"gd",@"gf",@"js",@"jt"];
    
    //小背景
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE.width - 40, 3 * SCREENSIZE.height / 4)];
    backView.userInteractionEnabled = YES;
    backView.layer.cornerRadius = 5.0;
    backView.clipsToBounds = YES;
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    
    
    CGFloat viewHeight = 3 * SCREENSIZE.height / 4;
    
    
    //    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE.width - 40, 30)];
    //标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENSIZE.width - 40, 2 * viewHeight / 14)];
    titleLabel.text = @"  --请选择银行--";
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.backgroundColor = setRGBColor(27, 155, 227);
    [backView addSubview:titleLabel];
    
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 2 * viewHeight / 14, SCREENSIZE.width - 40, 12 * viewHeight / 14)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self addSubview:_tableView];
    _tableView.backgroundColor = [UIColor whiteColor];
    
    //    配合cell中的方法设置分割线顶头
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [_tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
    }
    
    
}



#pragma mark - tableView dataSource && delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat viewHeight = 3 * SCREENSIZE.height / 4;
    CGFloat height = 11 * viewHeight / 14;
    return height / 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.row];
    }else if (indexPath.row == 1){
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.row];
    }else if (indexPath.row == 2){
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.row];
    }else if (indexPath.row == 3){
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.row];
    }else if (indexPath.row == 4){
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.row];
    }else if (indexPath.row == 5){
        cell.imageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.textLabel.text =  [_titleArray objectAtIndex:indexPath.row];
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    //    配合createTableView中的方法设置分割线顶头
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dic setObject:[_imageArray objectAtIndex:indexPath.row] forKey:@"image"];
    [dic setObject:[_titleArray objectAtIndex:indexPath.row] forKey:@"title"];
    [self.delegate chooseBankWithDic:dic];
    [self dismissChooseBank];
}


/**返回按钮事件*/
- (void)dismissChooseBank{
    [self removeFromSuperview];
}

- (void)removeFromSuperview{
    [self.backImageView removeFromSuperview];
    self.backImageView = nil;
    [UIView animateWithDuration:0.4f delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, 7 * SCREENSIZE.height / 8);
    } completion:^(BOOL finished) {
        [super removeFromSuperview];
    }];
}

/**显示view*/
- (void)show{
    UIViewController *topVC = [self appRootViewController];
    self.frame = CGRectMake(20, - 3 * SCREENSIZE.height / 4, SCREENSIZE.width - 40, 3 * SCREENSIZE.height / 4);
    [topVC.view addSubview:self];
}

/**获得当前的RootView*/
- (UIViewController *)appRootViewController{
    UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    while (appRootVC.presentedViewController) {
        appRootVC = appRootVC.presentedViewController;
    }
    return appRootVC;
}


- (void)willMoveToSuperview:(UIView *)newSuperview{
    if (newSuperview == nil) {
        return;
    }
    UIViewController *topVC = [self appRootViewController];
    
    //设置灰色背景
    if (!self.backImageView) {
        self.backImageView = [[UIView alloc] initWithFrame:topVC.view.bounds];
        self.backImageView.backgroundColor = [UIColor blackColor];
        self.backImageView.alpha = 0.4f;
        self.backImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;;
    }
    [topVC.view addSubview:self.backImageView];
    
    
    //设置出来时的动画
    [UIView animateWithDuration:0.35f delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.transform = CGAffineTransformMakeTranslation(0, 7 * SCREENSIZE.height / 8);
    } completion:^(BOOL finished) {
        [super willMoveToSuperview:newSuperview];
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
